package conchmusic.conchmusicclient.dao;

import java.util.List;

import conchmusic.conchmusicclient.pojo.Artist;
import conchmusic.conchmusicclient.pojo.Song;
import conchmusic.conchmusicclient.pojo.SongList;
import conchmusic.conchmusicclient.pojo.UpdatedData;
import conchmusic.conchmusicclient.pojo.User;

/**
 * Created by 76706 on 2017/4/23.
 */

public class ClientControl implements InterfaceControl{

    private OperateDB db;
    public ClientControl(MyDBHelp help) {
        db=new OperateDB(help);
    }
    @Override
    public void update(UpdatedData updatedData) {
        db.deleteSong_SongList();
        db.deleteSongList();
        User user=updatedData.getUser();
        List<Song> songs=updatedData.getSongs();
        List<SongList>songLists=updatedData.getSongLists();
        user.setIsLogin(1);
        if(db.findUser(user.getUserId())!=null){
            db.updateUser(user);
        }else{
            db.addUser(user);
        }
        for(int i=0;i<songs.size();i++){
            for(int j=0;j<songs.get(i).getArtistIds().size();j++) {
                Artist artist=new Artist(0,songs.get(i).getArtistIds().get(j),songs.get(i).getArtistNames().get(j),"",null,null);
                if(db.findArtist(artist.getArtistName())!=0){
                    db.updateArtist(artist);
                }else {
                    db.addArtist(artist);
                }
                if(db.findLocalSongId(songs.get(i).getSongId())!=0){
                    db.updateSong(songs.get(i));
                }else{
                    db.addSong(songs.get(i));
                }
                if(!db.findArtist_Song(db.findArtist(artist.getArtistName()),db.findLocalSongId(songs.get(i).getSongId())))
                {
                    db.addArtist_Song(db.findArtist(artist.getArtistName()),db.findLocalSongId(songs.get(i).getSongId()));
                }
            }
        }
        for(int i=0;i<songLists.size();i++){
            List<Integer> songIds=songLists.get(i).getSongIds();
            for(int j=0;j<songIds.size();j++){
                if(!db.findSong_SongList(songLists.get(i).getSongListId(),db.findLocalSongId(songIds.get(j)))) {
                    db.addSong_SongList(songLists.get(i).getSongListId(),db.findLocalSongId(songIds.get(j)));
                }
            }
            if(db.findSongList(songLists.get(i).getSongListId())==0) {
                db.addSongList(songLists.get(i));
            }else {
                db.updateSongList(songLists.get(i));
            }
        }
    }

    @Override
    public List<SongList> getSongLists(int userId) {
        List<SongList> songLists=db.findSongLists(userId);
        return songLists;
    }

    @Override
    public void addLocalSong(Song song) {
        operateSong(song,"addLocal");
    }

    @Override
    public List<Song> findSongs(int songListId, String title) {
        List<Song> songs=db.findSongs(songListId,title);
        return songs;
    }

    @Override
    public List<Song> findSongs(int songListId) {
        List<Song> songs=db.findSongs(songListId);
        return songs;
    }

    @Override
    public Song findSong(int localSongId) {
        Song song=db.findSong(localSongId);
        return song;
    }

    @Override
    public void deleteSong(int localSongId, int songListId) {
        db.deleteSong_SongList(localSongId,songListId);
    }

    @Override
    public void deleteOriginSong(int localSongId, int songListId) {
        db.deleteSong_SongList(localSongId,1);
        db.deleteSong_SongList(localSongId,2);
        db.deleteSong_SongList(localSongId,songListId);
        db.deleteSongUrl(localSongId);
    }

    @Override
    public void downLoadSong(Song song) {
        operateSong(song,"down");
    }

    @Override
    public User Login() {
        User user=db.findUser();
        return user;
    }

    @Override
    public User findUser(int userId) {
        User user=db.findUser(userId);
        return user;
    }

    @Override
    public boolean logout(int userId) {
        boolean a=false;
        if(db.logout(userId)){
            a=true;
        }
        return a;
    }

    @Override
    public void addSong(Song song) {
        operateSong(song,"add");
    }

    @Override
    public Song findCloudSong(Song song) {
        song.setLocalSongId(db.findLocalSongId(song.getSongId()));
        return song;
    }

    private void operateSong(Song song,String tip){
        int localSongId=db.findLocalSongId(song.getTitle(),song.getAlbumName());
        if(localSongId!=0 ){
            song.setLocalSongId(localSongId);
            db.updateSong(song);
            if(tip.equals("add")){

            }
            else{
                if(!db.findSong_SongList(1,localSongId)) {
                    db.addSong_SongList(1,localSongId);
                }
                if(tip.equals("down")){
                    if(!db.findSong_SongList(2,localSongId)){
                        db.addSong_SongList(2,localSongId);
                    }
                }
            }
        } else{
            db.addSong(song);
            int localSongId2=db.findLocalSongId(song.getTitle(),song.getAlbumName());
            for(int j=0;j<song.getArtistNames().size();j++) {
                Artist artist=new Artist(0,song.getArtistIds().get(j),song.getArtistNames().get(j),"",null,null);
                if(db.findArtist(artist.getArtistName())!=0){
                    db.updateArtist(artist);
                }else {
                    db.addArtist(artist);
                }
                if(!db.findArtist_Song(db.findArtist(artist.getArtistName()),localSongId2))
                {
                    db.addArtist_Song(db.findArtist(artist.getArtistName()),localSongId2);
                }
            }
            if(tip.equals("add")){

            }
            else{
                if(!db.findSong_SongList(1,localSongId2)) {
                    db.addSong_SongList(1,localSongId2);
                }
                if(tip.equals("down")){
                    if(!db.findSong_SongList(2,localSongId2)){
                        db.addSong_SongList(2,localSongId2);
                    }
                }
            }
        }
    }
}
